package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appPrdShowDao")
public class AppPrdShowDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppPrdShow queryWithKeys(String id) throws SQLException {
		AppPrdShow appPrdShow = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM  app_prd_show WHERE record_key=?");
			ps.setString(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				appPrdShow = new AppPrdShow();
				appPrdShow.setRecord_key(rs.getString("record_key"));
				appPrdShow.setApp_id(rs.getString("app_id"));
				appPrdShow.setPrd_name(rs.getString("prd_name"));
				appPrdShow.setPrd_file_path(rs.getString("prd_file_path"));
				appPrdShow.setLong_desc(rs.getString("long_desc"));
			}
		} catch (SQLException e) {
			throw new SQLException("query app_prd_show WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appPrdShow;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppPrdShow appPrdShow) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into app_prd_show(record_key, app_id, prd_name, prd_file_path, long_desc) values(?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appPrdShow.getRecord_key());
			ps.setString(2, appPrdShow.getApp_id());
			ps.setString(3, appPrdShow.getPrd_name());
			ps.setString(4, appPrdShow.getPrd_file_path());
			ps.setString(5, appPrdShow.getLong_desc());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert app_prd_show is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppPrdShow appPrdShow) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update app_prd_show set prd_name=?,prd_file_path=?,long_desc=? where record_key=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appPrdShow.getPrd_name());
			ps.setString(2, appPrdShow.getPrd_file_path());
			ps.setString(3, appPrdShow.getLong_desc());
			ps.setString(4, appPrdShow.getRecord_key());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update app_prd_show WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String appid, String recordKey) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from app_prd_show where record_key=? and app_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, recordKey);
			ps.setString(2, appid);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete app_prd_show WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
